﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#ifndef CSV_FILE_MANAGER
#define  CSV_FILE_MANAGER

#include <string>
#include <iostream>
#include "IDocument.h"
#include "IFamilyDatabase.h"
#include "ICSVFileManager.h"
#include "ExprNode.h"
namespace gfam
{
    class  CSVFileManager : public gfam::ICSVFileManager
    {
    public:
        CSVFileManager(gcmp::IDocument* pDoc);
        ~CSVFileManager();

        virtual int GetFileCount() const override;
        virtual std::wstring GetFileNameByIndex(int index) const override;
        virtual int GetFileIndexByName(const std::wstring name) const override;
        virtual bool IsCSVFileExist(const std::wstring& fileName) const override;
        virtual bool DeleteCSVFile(const std::wstring& fileName, std::wstring& error) override;
        virtual bool ImportCSVFile(const std::wstring& fileName, const std::wstring& filePath, std::wstring& error) override;
        virtual bool ExportCSVFile(const std::wstring& fileName, const std::wstring& filePath, std::wstring& error) override;
        virtual bool LoadDefaultCSVFile(const std::wstring& defaultFileDir,std::wstring& error) override;

    private:
        bool ParseCSVFile(const std::wstring& name, const std::wstring& filePath, std::vector<std::pair<std::wstring, gfam::FamilyDataType>>&columnList, std::vector<std::vector<std::wstring>>&values);
        bool WriteCSVFile(const std::wstring& name, const std::wstring& filePath, std::vector<std::pair<std::wstring, gfam::FamilyDataType>>&columnList, std::vector<std::vector<std::wstring>>&values);
        gfam::FamilyDataType GetTypeByName(std::wstring& name, const std::wstring& seperator);
        std::shared_ptr<gfam::IFamilyDataTable> GetTableByName(const std::wstring& tableName);
        bool IsCSVFileUsed(const std::wstring& fileName);
        bool CheckExprNode(ExprNode* exprNode, const std::wstring & fileName);

    private:
        std::shared_ptr<gfam::IFamilyDatabase>        m_db = nullptr;
        gcmp::IDocument*                              m_pDoc = nullptr;
    };
}

#endif